<!DOCTYPE html>
<html lang="en" >
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/BlogAdmin/layui/css/layui.css" media="all">
</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend style="text-align: center">iArchie博客</legend>
</fieldset>
<div style="padding: 20px; background-color: #F2F2F2;">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">评论管理信息</div>
                <form action="" method="post">
                    <div class="layui-card-body">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">ID:</label>
                                <div class="layui-input-inline">
                                    <input type="text"  name="queryBut" placeholder="请输入需要查询的ID"
                                           lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">评论者:</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="queryBut" placeholder="请输入需要查询的评论名"
                                           lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">评论内容:</label>
                                <div class="layui-input-inline">
                                    <input type="text"  name="queryBut" placeholder="请输入需要查询的评论内容"
                                           lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <button class="layui-btn layui-btn-warm layui-btn-radius">点击查询</button>
                        </div>
                        <table id="demo" lay-filter="test"></table>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script src="/BlogAdmin/layui/layui.js"></script>
<script src="/LayuiBlog/js/jquery-min.js"></script>
<script>
    layui.use('table', function () {
        var table = layui.table;

        //第一个实例
        table.render({
            elem: '#demo'
            , height: 600
            , url: '/admin/getCommentsMang'
            , page: true
            ,parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": 0, //解析接口状态
                    "msg": "", //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": res.data //解析数据列表
                };
            }
            , request: {
                pageName: 'pageNum', //页码的参数名称，默认：page
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
            , cols: [
                        [
                {type: 'checkbox', fixed: 'left'}
                , {field: 'commentId', title: '评论ID', width: 90, sort: true}
                , {field: 'parentCommentId', title: '评论父ID', width: 130, sort: true}
                , {field: 'nickname', title: '评论者昵称', width: 150}
                , {field: 'conmentcontent', title: '评论内容', width: 230}
                , {field: 'blogId', title: '文章ID', width: 150, sort: true}
                , {field: 'avatar', title: '评论者头像', width: 150}
                , {field: 'location', title: '评论地点', width: 130}
                , {field: 'createTime', title: '评论时间', width: 150, sort: true}
                , {
                    fixed: 'right',
                    title: '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;操作',
                    toolbar: '#barDemo',
                    width: 180
                }
                    ]
                ]
        });
        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'del') {
                layer.confirm('真的删除ID为：'+data.commentId+"的评论吗？", function (index) {
                    $.ajax({
                        type: 'post',
                        url: "/admin/deleteCommentByCommentId",
                        // contentType: "application/x-www-form-urlencoded",
                        dataType: "json",
                        data:{
                            commentId:data.commentId
                        },
                        success: function (res) {
                            if (res.result == 1) {
                                layer.msg("删除评论成功！", {icon: 6});
                            } else {
                                layer.msg("删除评论失败！", {icon: 5});
                            }
                        },
                        error: function () {
                            layer.msg("删除评论失败！", {icon: 5});
                        }
                    })
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                layer.open({
                    type: 2,
                    area: ['45%', '400px'],
                    title: '编辑评论',
                    skin: 'layui-layer-rim', //加上边框
                    btn: ['保存', '取消'],
                    btnAlign: 'c',
                    top: 100,
                    content: ['/admin/layer_model4', 'no'],
                    // content:$("#popUpdateTest") //引用的弹出层的页面层的方式加载修改界面表单
                    success: function (layero,index) {
                        var body=layui.layer.getChildFrame('body',index);
                        body.find("[name=commentId]").val(data.commentId);
                        body.find("[name=parentCommentId]").val(data.parentCommentId);
                        body.find("[name=commentAvatar]").val(data.avatar);
                        body.find("[name=commentContent]").val(data.conmentcontent);
                    },
                    yes:function (index) {
                        var body=layui.layer.getChildFrame('body',index);
                        var commentId=body.find("[name=commentId]").val();
                        var parentCommentId=body.find("[name=parentCommentId]").val();
                        var commentAvatar=body.find("[name=commentAvatar]").val();
                        var commentContent=body.find("[name=commentContent]").val();
                        var blogId=data.blogId;
                        var location=data.location;
                        var nickname=data.nickname;
                        $.ajax( {
                            url:"/admin/updateCommentByCommentId",
                            dataType:"json",
                            type:"POST",
                            data:{
                                commentId:commentId,
                                parentCommentId:parentCommentId,
                                commentAvatar:commentAvatar,
                                commentContent:commentContent,
                                blogId:blogId,
                                location:location,
                                nickname:nickname
                            },
                            success: function (res) {
                                if (res.result == 1) {
                                    obj.update({
                                        commentId:commentId,
                                        parentCommentId:parentCommentId,
                                        commentAvatar:commentAvatar,
                                        commentContent:commentContent,
                                        blogId:blogId,
                                        location:location,
                                        nickname:nickname
                                    });
                                    layer.msg("更新博客文章成功！", {icon: 6});
                                    layer.close(index);
                                } else {
                                    layer.msg("更新博客文章失败！", {icon: 5});
                                }
                            }
                        })
                    }
                });
            }
        });
    })
    ;

</script>
</body>
</html>